package cn.pku.edu.cs.ldn.util;

import android.net.Uri;
import android.util.Log;

public class NetworkUtil {
	private static final String TAG = "NetworkUtil";
	private static final boolean DEBUG = true;

	/**
	 * try to add "http://" prefix to the url if it does not contain
	 * "http","https" or "ftp".
	 * 
	 * @param url
	 * @return
	 */
	public static String getCanonicalUrl(String url) {
		if (url == null) {
			return null;
		}
		if (url.startsWith("/")) {
			if (DEBUG)
				Log.v(TAG, "getCanonicalUrl: seems to be a local path " + url);
			return url;
		}
		Uri uri = Uri.parse(url);
		if (uri.getScheme() == null) {
			url = "http://" + url;
		}
		return url;
	}

	public static boolean isIdentical(String url1, String url2) {
		url1 = getCanonicalUrl(url1);
		url2 = getCanonicalUrl(url2);
		if (url1 == null || url2 == null) {
			return false;
		}
		return url1.equals(url2);
	}

}
